import 'package:flutter/material.dart';

import './constants.dart' show AppFonts, AppColors;

import './wechat_page.dart' show WechatPage;

import './contact_page.dart' show ContactPage;

enum ActionItems {
  GROUP_WECHAT,ADD_FRIEND,QR_SCAN,PAYMENT,HELP
}

// 创建动态组件
class HomeScreen extends StatefulWidget{
  _HomeScreenState createState() => _HomeScreenState();
}

// 动态组件的私有方法
class _HomeScreenState extends State<HomeScreen>{

  // 页面滚动控制
  PageController _pageController;
  // 页面集合
  List<Widget> _pages;
  // 声明 NavigationIconView集合
  List<NavigationIconView> _navigation_icon_views;
  // 初始化数据
  void initState(){
    super.initState();
    _navigation_icon_views = [
      NavigationIconView(
          title: '微信',
          icon: new IconData(
            0xe600,
            fontFamily:AppFonts.AppIconFontsFamily
          ),
          activeIcon: new IconData(
            0xe61b,
            fontFamily: AppFonts.AppIconFontsFamily
          )
      ),
      NavigationIconView(
          title: '通讯录',
          icon: new IconData(
            0xe602,
            fontFamily:AppFonts.AppIconFontsFamily
          ),
          activeIcon: new IconData(
            0xe601,
            fontFamily: AppFonts.AppIconFontsFamily
          )
      ),
      NavigationIconView(
          title: '发现',
          icon: new IconData(
            0xe786,
            fontFamily: AppFonts.AppIconFontsFamily
          ),
          activeIcon: new IconData(
            0xe788,
            fontFamily: AppFonts.AppIconFontsFamily
          )
      ),
      NavigationIconView(
          title: '我',
          icon: new IconData(
            0xe7ab,
            fontFamily: AppFonts.AppIconFontsFamily
          ),
          activeIcon: new IconData(
            0xe632,
            fontFamily: AppFonts.AppIconFontsFamily
          )
      ),
    ];
    _pageController = new PageController(initialPage:_currentIndex);
    _pages = [
      new WechatPage(),
      new ContactPage(),
      new Container(
        child:new Text('3'),
        color:Colors.deepPurple
      ),
      new Container(
        child:new Text('4'),
        color:Colors.green
      ),
    ];
  }

  // 构建下拉列表图标文字行
  _buildPopupMenuItemRow(icon,String title){
    var row = new Row(
      children:<Widget>[
        new Container(
          child:new Icon(
            new IconData(
                icon,
                fontFamily:AppFonts.AppIconFontsFamily
            ),
            color:Colors.white
//            size:20.0,
          ),
          padding:const EdgeInsets.only(right:15.0,left:5.0)
        ),
        new Text(
          title,
          style:TextStyle(
            color:Colors.white
          )
        )
      ],
    );
    return row;
  }

  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar bottomNavBar = new BottomNavigationBar(
      items: _navigation_icon_views.map(
          (NavigationIconView item) {
            return item.item;
          }
      ).toList(),
      onTap:(int index){

        setState((){
          _currentIndex = index;
//          _pageController.animateToPage(
//              _currentIndex,
//            duration:new Duration(milliseconds: 10),
//            curve:Curves.ease
//          );
          _pageController.jumpToPage(index);
        });
      },
      currentIndex:_currentIndex,
      type:BottomNavigationBarType.fixed,
      fixedColor:new Color(AppColors.ActiveBottomIcon),
    );
    return Scaffold(
      appBar:new AppBar(
        elevation:0.0,
        title:new Text('微信'),
        actions:[
          new Container(
            child:new IconButton(
                icon: new Icon(Icons.search),
                onPressed: (){
                  print('搜索');
                },
                iconSize:30.0,
            ),
            padding:const EdgeInsets.only(right:30.0),
          ),
          new PopupMenuButton(
            itemBuilder:(BuildContext cxt){
              return <PopupMenuEntry<ActionItems>>[
                new PopupMenuItem(
                  child: _buildPopupMenuItemRow(0xe61b,'发起群聊'),
                  value:ActionItems.GROUP_WECHAT,
                ),
                new PopupMenuItem(
                  child:_buildPopupMenuItemRow(0xe61d,'添加朋友'),
                  value:ActionItems.ADD_FRIEND
                ),
                new PopupMenuItem(
                  child:_buildPopupMenuItemRow(0xe634,'扫一扫'),
                  value:ActionItems.QR_SCAN
                ),
                new PopupMenuItem(
                  child:_buildPopupMenuItemRow(0xe62a,'收付款'),
                  value:ActionItems.PAYMENT
                ),
                new PopupMenuItem(
                  child: _buildPopupMenuItemRow(0xe648,'帮助与反馈'),
                  value:ActionItems.HELP
                )
              ];
            },
            onSelected:(ActionItems str){
              print(str);
            },
            icon:new Icon(
              Icons.add_circle_outline,
              size: 30.0,
            ),
            padding:const EdgeInsets.only(right:30.0),
          )
        ]
      ),
      body:new PageView.builder(
          itemBuilder: (BuildContext ctx,int index){
            print(_currentIndex);
            return _pages[index];
          },
        itemCount: _pages.length,
        controller:_pageController,
        onPageChanged: (int index){
            setState((){
              _currentIndex = index;
            });
        },
      ),
      bottomNavigationBar: bottomNavBar
    );
  }
}
// 创建底部标签类
class NavigationIconView {
  final BottomNavigationBarItem item;
//  final String _title;
//  final Widget _icon;
//  final Widget _activeIcon;

  NavigationIconView({
    Key key,
    String title,
    IconData icon,
    IconData activeIcon
  }) :
//  _title = title,
//  _icon = icon,
//  _activeIcon = activeIcon,
  item = new BottomNavigationBarItem(
    icon: new Icon(icon, color:new Color(AppColors.BottomIcon)),
    activeIcon: new Icon(activeIcon),
    title: new Text(title,style:TextStyle(fontSize: 14.0)),
    backgroundColor: Colors.lightBlue
  );
}